/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.mushroom.website.daoImpl;


import cz.muni.fi.pa165.mushroom.website.dao.GathererDao;
import cz.muni.fi.pa165.mushroom.website.entities.Gatherer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.codec.digest.DigestUtils;

/**
 * @author Pavel Démuth
 */
@Repository
public class GathererDaoImpl implements GathererDao {

    @PersistenceContext
    private EntityManager em;


    public void createGatherer(Gatherer gatherer) {
        checkGatherer(gatherer);

        if (gatherer.getId() != null) throw new IllegalArgumentException("Gatherer ID is already set");        
        
        em.persist(gatherer);
    }

    public void updateGatherer(Gatherer gatherer) {
        checkGatherer(gatherer);

        if (gatherer.getId() == null) throw new IllegalArgumentException("Gatherer has no ID");        
       
        em.merge(gatherer);
    }

    public void removeGatherer(Gatherer gatherer) {
        if (gatherer == null) throw new IllegalArgumentException("Gatherer is null");
        if (gatherer.getId() == null) throw new IllegalArgumentException("Gatherer has no ID");

        em.remove(em.contains(gatherer) ? gatherer : em.merge(gatherer)); 
    }

    public Gatherer findGatherer(Long id) {
        if (id == null) throw new IllegalArgumentException("Gatherer ID is null");

        return em.find(Gatherer.class, id);
    }
    
    public Gatherer findGathererByUsername(String username) {
        if (username == null) throw new IllegalArgumentException("Username is null");        

        TypedQuery<Gatherer> query = em.createQuery("SELECT g FROM Gatherer g WHERE g.username = ?1", Gatherer.class);
        query.setParameter(1, username);   
          
        return query.getSingleResult();
    }

    public List<Gatherer> listGatherers() {
        TypedQuery<Gatherer> query = em.createQuery("SELECT g FROM Gatherer g", Gatherer.class);
        return query.getResultList();
    }

    public Gatherer confirmLogin(String username, String password) {
        if (username == null) throw new IllegalArgumentException("Username is null");
        if (password == null) throw new IllegalArgumentException("Password is null");

        TypedQuery<Gatherer> query = em.createQuery("SELECT g FROM Gatherer g WHERE g.username = ?1 AND g.password = ?2", Gatherer.class);
        query.setParameter(1, username);     
        String sha256hex = DigestUtils.sha256Hex(password); 
            query.setParameter(2, sha256hex);
        
        return query.getSingleResult();
    }

    private void checkGatherer(Gatherer gatherer) throws IllegalArgumentException {
        if (gatherer == null) throw new IllegalArgumentException("Gatherer is null");
        if (gatherer.getFirstName() == null) throw new IllegalArgumentException("Gatherer has no firstname");
        if (gatherer.getLastName() == null) throw new IllegalArgumentException("Gatherer has no lastname");
        if (gatherer.getUsername() == null) throw new IllegalArgumentException("Gatherer has no username");
        if (gatherer.getPassword() == null) throw new IllegalArgumentException("Gatherer has no password");
    }
  
}
